
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
local FlightSound = Sound( "PhysicsCannister.ThrusterLoop" )
function ENT:Initialize()

	self.Entity:SetModel( "models/weapons/w_missile_launch.mdl" )
	self.Entity:SetName("Missile")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:SetMass(1000)
	end
	self.Entity:EmitSound( FlightSound )
	self.PhysObj = self.Entity:GetPhysicsObject()
	self.LTime = CurTime() + 1
	self.DTime = CurTime() + 10
	RockTrail = ents.Create("env_rockettrail")
				RockTrail:SetPos( self.Entity:GetPos() )
				RockTrail:SetParent(self.Entity)
				RockTrail:SetAngles( self.Entity:GetUp():Angle() + Angle(90)  )
				RockTrail:Spawn()
				RockTrail:Activate()

end

function ENT:PhysicsUpdate()

	if(self.DTime < CurTime()) then
		self.Entity:Remove()
		return
	end

	
		if (CurTime() > self.LTime) then
			self.Target = Vector(self.ParL.XCo, self.ParL.YCo, self.ParL.ZCo)
			AimVec = ( self.Target - self.Entity:GetPos() ):Angle()
			self.Entity:GetPhysicsObject():SetAngle(AimVec)
		end
	
		self.PhysObj:SetVelocity(self.Entity:GetForward()*3100)

end


function ENT:StartTouch( ent )

	if ent:GetClass() == sheild then
			local effectdata = EffectData()
				effectdata:SetOrigin(self.Entity:GetPos())
				effectdata:SetStart(self.Entity:GetPos())
				util.Effect( "Explosion", effectdata )
				self.Entity:Remove()
	else
	
		gcombat.hcgexplode( self.Entity:GetPos(), 300 , self.ParL.power, 8)
		util.BlastDamage(self.Entity, self.Entity,self.Entity:GetPos(), 300, 100)
		local effectdata = EffectData()
		effectdata:SetOrigin(self.Entity:GetPos())
		effectdata:SetStart(self.Entity:GetPos())
		util.Effect( "Explosion", effectdata )
		self.Entity:Remove()
	end

end

function ENT:OnRemove()
self.Entity:StopSound( FlightSound )
end